﻿/*
·´`·.¸.»------------------------------------------------------------ë·´`·.¸.»
        File: UnsafeBlackOutAlgorithm.cs
        Project: Themaopdracht 7
        Author: Felix Mann

        Description:
        This algorithm fills up a bitmap with black pixels. It is very
        similar to BlackOutAlgorithm except that the pixel access is 
        done in an unsafe fashion thus saving a little of precious CPU time.
·´`·.¸.»------------------------------------------------------------ë·´`·.¸.»
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class UnsafeBlackout : VisionAlgorithm
    {
        public UnsafeBlackout(String name) : base(name) { }
        public unsafe override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);
            BitmapData bitmapData = returnImage.LockBits(new Rectangle(0, 0, returnImage.Width, returnImage.Height), ImageLockMode.ReadWrite, returnImage.PixelFormat);
            byte bitsPerPixel = Convert.ToByte(32);
            byte* scan0 = (byte*)bitmapData.Scan0.ToPointer();
            for (int i = 0; i < bitmapData.Height; ++i)
            {
                for (int j = 0; j < bitmapData.Width; ++j)
                {
                    byte* data = scan0 + i * bitmapData.Stride + j * bitsPerPixel / 8;
                    data[0] = 0;
                    data[1] = 0;
                    data[2] = 0;
                }
            }

            returnImage.UnlockBits(bitmapData);
            return returnImage;
        }
    }
}
